<template>
  <div class="vote-box">
    <el-row
      type="flex"
      justify="center"
      align="middle"
    >
      <el-col :span="18">
        <el-progress
          :percentage="percentage"
          :show-text="type === 'line'"
        ></el-progress>
      </el-col>
      <el-col :span="6">{{ quantity }}票</el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { computed } from "vue";

interface VoteItem {
  value: string | number;
  quantity: number;
}

interface Props {
  value?: string | number;
  totalVote?: number;
  type?: string;
  voteList?: VoteItem[];
}

const props = withDefaults(defineProps<Props>(), {
  value: "",
  totalVote: 0,
  type: "line",
  voteList: () => []
});

defineEmits<{
  (e: "update:value", value: string | number): void;
}>();

const quantity = computed(() => {
  const index = props.voteList.findIndex(item => props.value == item.value);
  return props.voteList[index] ? props.voteList[index].quantity : 0;
});

const percentage = computed(() => {
  if (quantity.value === 0) {
    return 0;
  }
  // 计算百分比 = 投票数 / 总票数
  return Math.floor((quantity.value / props.totalVote) * 100);
});
</script>

<style lang="scss" scoped>
.vote-box {
  font-size: 14px;
  line-height: 25px;
  color: var(--form-theme-color);
  width: 96%;

  :deep(.el-progress__text) {
    color: var(--form-theme-color) !important;
  }
}
</style>
